Login and get codingBill is looking for a new apartment with good view, staying away from the crowed city.
He has been working with some agents and has piles of data of all sorts of buildings. However he is too busy to go through the data. Can you help him by designing a fast algorithm to search for the best possible apartment(s) with the desired view?
The requirement: find an apartment with either sun set or sun rise view. Given an list of buildings in height (floors), and desired view, either East (sun rise) or West (sun set), you have to find all the candidate buildings and return them by their position (index) in the passed in building
list
.An example
Here is a visual representation when
buildings
input argument is[3, 5, 4, 3, 3, 1]
(eachint
represents the height of the building and note that the buildings are in one line with no gaps in between):
_
West | |_ East
_| | |_ _
| | | | | |
| | | | | |_
|_|_|_|_|_|_|Expected output for this buildings input list:
- Given direction
WEST
('W'
) -> the function returns[0, 1]
= indices of buildings that can see the sun set.- Given direction
EAST
('E'
) -> the function returns[1, 2, 4, 5]
= indices of buildings that can see the sun rise.Example running the function in the REPL
>>> search_apartment([3, 5, 4, 3, 3, 1], 'W')
[0, 1]
>>> search_apartment([3, 5, 4, 3, 3, 1], 'E')
[1, 2, 4, 5]
>>> search_apartment([1, 1, 1, 1, 1], 'W')
[0]
>>> search_apartment([2, 1, 1, 1, 1], 'E')
[0, 4]See the accompanying tests for more examples.
Good luck and keep calm and code in Python!
53 out of 54 users completed this Bite.
Will you be the 54th person to crack this Bite?
Resolution time: ~50 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 6.67 on a 1-10 difficulty scale.
» Up for a challenge? 💪